package com.example.saas.datasource;

import com.baomidou.dynamic.datasource.provider.AbstractJdbcDataSourceProvider;
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.example.saas.config.properties.MasterDataSourceProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

/**
 * 写法一：动态数据源加载类-基于Mybatis-plus的DynamicDataSourceProvider进行扩展
 */
@Slf4j
@Configuration
public class DbDynamicDataSourceProvider {

    @Resource
    private MasterDataSourceProperties defaultDsProperties;

    @Bean
    public DynamicDataSourceProvider dynamicDataSourceProvider(){
         return new AbstractJdbcDataSourceProvider(defaultDsProperties.getDriverClassName(), defaultDsProperties.getUrl(), defaultDsProperties.getUsername(), defaultDsProperties.getPassword()) {
             @Override
             protected Map<String, DataSourceProperty> executeStmt(Statement statement) {
                 Map<String, DataSourceProperty> dataSourceMap = new HashMap<>();
                 ResultSet resultSet = null;
                 try {
                     dataSourceMap = new HashMap<>();
                     resultSet = statement.executeQuery("SELECT * FROM SYS_TENANT_DATASOURCE");
                     while (resultSet.next()){
                         String key = resultSet.getString("key");
                         String driverClass = resultSet.getString("driver_class_name");
                         String url = resultSet.getString("url");
                         String username = resultSet.getString("username");
                         String password = resultSet.getString("password");
                         DataSourceProperty dataSourceProperty = new DataSourceProperty();
                         dataSourceProperty.setDriverClassName(driverClass);
                         dataSourceProperty.setUrl(url);
                         dataSourceProperty.setUsername(username);
                         dataSourceProperty.setPassword(password);
                         dataSourceMap.put(key, dataSourceProperty);
                     }
                 }catch (Exception e){
                     log.error("加载数据源失败,错误信息：{}", e.getMessage());
                 }finally {
                     try {
                         if (resultSet != null) {
                             resultSet.close();
                         }
                     } catch (SQLException e) {
                         log.error("关闭数据库连接资源失败,错误信息：{}", e.getMessage());
                     }
                     try {
                         statement.close();
                     } catch (SQLException e) {
                         log.error("关闭数据库连接资源失败,错误信息：{}", e.getMessage());
                     }
                 }
                 return dataSourceMap;
             }
         };
    }
}
